package com.cta.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cta.constant.ROLE;
import com.cta.dao.SitePersistence;
import com.cta.entity.Contact;
import com.cta.entity.PBX;
import com.cta.entity.ServerConfig;
import com.cta.entity.Site;
import com.cta.utils.SupportLoaderDelegate;

@Repository("sitePersistence")
public class SitePersistenceImpl extends BasePersistenceImpl implements SitePersistence {

	/**
	 * find all Sites
	 * 
	 * @return
	 */
	public List<Site> findAllSites() {
		return getList("select s from Site s");
	}

	/**
	 * find all child site of parent id
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Site> findAllChildSite(Long parentId) {
		return getList("select s from Site s where s.parentId = ?1 order by s.id", parentId);
	}

	/**
	 * find root Site
	 * 
	 * @return
	 */
	public Site findRootSite() {
		List<Site> list = getList("select s from Site s where s.parentId is null");
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	/**
	 * find site by name
	 * @param name
	 * @return
	 */
	public Site findSiteByName(String name) {
		List<Site> list = getList("select s from Site s where lower(s.name) = ?1", name.toLowerCase());
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	/**
	 * find all Contact by parentId( assignTo )
	 * @param parentId
	 * @return
	 */
	public List<Contact> findContactByParentId(Long parentId) {
		return getList("select c from Contact c where c.assignTo = ?1", parentId);
	}
	
	/**
	 * find all contact except super admin
	 * @return
	 */
	public List<Contact> findAllContactExceptSuperAdmin() {
		return getList("select c from Contact c where c.role.id = ?1 or c.role.id =?2", ROLE.ADMIN.getId(), ROLE.CONTACT.getId());
	}
	
	/**
	 * find all contact except super admin
	 * @return
	 */
	public List<Contact> findAllContactExceptSuperAdminAndNotAssign() {
		return getList("select c from Contact c where (c.role.id = ?1 or c.role.id =?2) and c.assignTo is NULL", ROLE.ADMIN.getId(), ROLE.CONTACT.getId());
	}
	
	/**
	 * get all admin contact
	 * @return
	 */
	public List<Contact> findAllAdminContact() {
		return getList("select c from Contact c where c.role.id = ?1", ROLE.ADMIN.getId());
	}
	
	/**
	 * get all normal contact
	 * @return
	 */
	public List<Contact> findAllNormalContact() {
		return getList("select c from Contact c where c.role.id = ?1", ROLE.CONTACT.getId());
	}
	
	/**
	 * find all PBX by parentId (assignTo)
	 * @param parentId
	 * @return
	 */
	public List<PBX> findPBXByParentId(Long parentId) {
		return getList("select p from PBX p where p.assignTo = ?1 ", parentId);
	}
	
	/**
	 * find all PBX with eager service factory load
	 * @param parentId
	 * @return
	 */
	public List<PBX> findAllPBX(SupportLoaderDelegate<PBX> loader) {
		return getList("select p from PBX p", loader);
	}
	
	/**
	 * find all PBX which are not assigned to site node and  eager service factory load
	 * @param parentId
	 * @return
	 */
	public List<PBX> findAllPBXNotAssign(SupportLoaderDelegate<PBX> loader) {
		return getList("select p from PBX p where p.assignTo is null", loader);
	}
	
	/**
	 * find pbx by factory id
	 * @param factoryId
	 * @return
	 */
	public List<PBX> findPBXByFactoryId(Long factoryId) {
		return getList("select p from PBX p where p.pbxFactory.id = ?1 ", factoryId);
	}
	
	public ServerConfig loadConfig(){
		List<ServerConfig>  svs = getList("select s from ServerConfig s");
		if(svs.size() > 0){
			return svs.get(0);
		}
		return null;
	}
	
	

}
